package com.bodu.core.util;

import com.bodu.core.entity.enums.SendEmailTypeEnum;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Objects;
import java.util.Properties;

/**
 * 发Email
 *
 * @author:Administrator
 * @create:2022-04-22 14:59
 */
public class SendEmailUtils {

    /**
     * 连接协议
     */
    private static final String PROTOCAL_KEY = "mail.transport.protocol";
    private static final String PROTOCAL_VAL = "smtp";

    /**
     * 连接的主机
     */
    private static final String SMTP_HOST_KEY = "mail.smtp.host";

    /**
     * 连接的主机端口
     * // 连接的主机端口VAL 465,或者 587， 一般使用465 ，如果465不通，换成587
     */
    private static final String SMTP_POST_KEY = "mail.smtp.port";
    private static final Integer SMTP_POST_VAL = 465;

    /**
     * 是否使用权限认证
     * // 开启权限认证 true- 开启， false - 关闭
     */
    private static final String STMP_AUTH_KEY = "mail.smtp.auth";
    private static final String STMP_AUTH_VAL = "true";

    /**
     * 是否开启SSL
     * // 开启权限认证 true- 开启， false - 关闭
     */
    private static final String STMP_SSL_KEY = "mail.smtp.ssl.enable";
    private static final String STMP_SSL_VAL = "true";

    /**
     * 是否显示debug信息
     * // 开启权限认证 true- 开启， false - 关闭
     */
    private static final String OPEN_MAIL_DEBUG_KEY = "mail.debug";
    private static final String OPEN_MAIL_DEBUG_VAL = "true";

    /**
     * 发送文本Email 信息
     *
     * @param emailType     发送邮件类型
     * @param sendEmail     发送方账号
     * @param sendCode      发送方Code
     * @param receiverEmail 接收方账号
     * @param title         发送标题
     * @param content       发送内容
     * @return void
     */
    public void sendTxt(Integer emailType, String sendEmail, String sendCode, String receiverEmail, String title, String content) throws javax.mail.MessagingException {
        SendEmailTypeEnum sendEmailTypeEnum = null;
        if (Objects.equals(SendEmailTypeEnum.SINA_COM.getKey(), emailType)) {
            sendEmailTypeEnum = SendEmailTypeEnum.SINA_COM;
        } else if (Objects.equals(SendEmailTypeEnum.SINA_CN.getKey(), emailType)) {
            sendEmailTypeEnum = SendEmailTypeEnum.SINA_CN;
        }

        //把收件人放入对象中
        Session session = createSendEmailSession(sendEmailTypeEnum.getStmp());
        Message message = createMessage(session, sendEmail, title, content);
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
        sendMail(session, sendEmail, sendCode, message);
    }

    /**
     * 生成session
     *
     * @return
     */
    private static Session createSendEmailSession(String stmphost) {
        Properties properties = new Properties();
        properties.put(PROTOCAL_KEY, PROTOCAL_VAL);
        properties.put(SMTP_HOST_KEY, stmphost);
        properties.put(SMTP_POST_KEY, SMTP_POST_VAL);
        properties.put(STMP_AUTH_KEY, STMP_AUTH_VAL);
        properties.put(STMP_SSL_KEY, STMP_SSL_VAL);
        properties.put(OPEN_MAIL_DEBUG_KEY, OPEN_MAIL_DEBUG_VAL);
        return Session.getInstance(properties);
    }

    /**
     * 创建message对象
     *
     * @param session
     * @param title
     * @param content
     * @return
     * @throws javax.mail.MessagingException
     */
    private static Message createMessage(Session session, String sendEmail, String title, String content) throws javax.mail.MessagingException {
        // 获取邮件对象
        Message message = new MimeMessage(session);
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress(sendEmail));
        // 设置邮件标题
        message.setSubject(title);
        // 设置邮件内容
        message.setContent(content, "text/html;charset=gb2312");
        return message;
    }

    /**
     * 发送邮件
     *
     * @param session
     * @param message
     * @throws MessagingException
     */
    private static void sendMail(Session session, String sendEmail, String sendCode, Message message) throws javax.mail.MessagingException {
        // 得到邮差对象
        Transport transport = session.getTransport();
        // 连接自己的邮箱账户
        // 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
        transport.connect(sendEmail, sendCode);
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
